package com.lagou;

import com.lagou.bean.ConsumerComponent;
import org.apache.commons.lang3.RandomUtils;
import org.apache.dubbo.config.spring.context.annotation.EnableDubbo;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.PropertySource;

import java.io.IOException;
import java.util.concurrent.ScheduledThreadPoolExecutor;
import java.util.concurrent.TimeUnit;


public class AnnotationConsumerMain {
    public static void main(String[] args) throws IOException, InterruptedException {
        AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(ConsumerConfiguration.class);
        context.start();
        ConsumerComponent service = context.getBean(ConsumerComponent.class);
//        while (true) {
//            System.in.read();
//            try {
//                String hello = service.sayHello("world", 0);
//                System.out.println("result :" + hello);
//            } catch (Exception e) {
//                e.printStackTrace();
//            }
//        }

        ScheduledThreadPoolExecutor scheduled = new ScheduledThreadPoolExecutor(10);
        scheduled.scheduleAtFixedRate(new Runnable() {
            @Override
            public void run() {
                String hello;
                switch (RandomUtils.nextInt(1,4)) {
                    case 1:
                        hello = service.sayHello("world", 0);
                        break;
                    case 2:
                        hello = service.sayHello2("world");
                        break;
                    case 3:
                        hello = service.sayHello3("world");
                        break;
                    default:
                        hello = service.sayHello("world", 0);
                }

                System.out.println("result :" + hello);
            }
        }, 0, 5, TimeUnit.SECONDS);
    }

    @Configuration
    @PropertySource("classpath:/dubbo-consumer.properties")
    //@EnableDubbo(scanBasePackages = "com.lagou.bean")
    @ComponentScan("com.lagou.bean")
    @EnableDubbo
    static class ConsumerConfiguration {

    }
}
